#!/bin/bash

###
### Program: timemachine
### File:    tm-quota.sh
### Version: 2.0
### Owner:   raptus ag
### Author:  Florian S.
### Purpose:
###
### Check the configured quotas and report, but take no action
### Two modes are supported: Showing Stats, Showing Quotas
###

#
# standard stuff
#

#!/bin/bash

SHOW_QUOTA=""
SHOW_STATS=""
SHOW_HISTORY=""
CONFIGFILE=""

ME=`basename $0 | head -c -4`

if test "$#" -eq 0; then 
    echo "Usage: $0 [history|quota|stats] [<config-file>]"
    exit
fi

for i in "$@"
do
    case "$i" in
        history)
            SHOW_HISTORY="yes"
            ;;

        stats)
            SHOW_STATS="yes"
            ;;

        quota)
            SHOW_QUOTA="yes"
            ;;

        *)
            IS_CONF=`echo $i | grep ".conf"`
            if ! test -z "$IS_CONF"
            then
                CONFIGFILE="$i"
            else
                echo "Usage: $0 [history|quota|stats] [<config-file>]"
                exit
            fi
            ;;
    esac
done

SCRIPT=$(readlink -f $0)
SCRIPTPATH=`dirname $SCRIPT`
OLDDIR="`pwd`"
cd $SCRIPTPATH

if ! test -z "$CONFIGFILE"
then
    if ! test -f "$CONFIGFILE"
    then
        echo "ERROR: Cannot find config file $CONFIGFILE"
        exit 1
    fi
fi

JOBNAME="statistics"
source "../config/defaults.conf"
if ! test -z "$CONFIGFILE"; then source "$CONFIGFILE"; fi
source "functions/logging.inc.sh"


log "-----------------------------------------------------------------------"
log "This is $CUSTOMER $HOSTNAME timemachine statistics"


# testing for gawk

if test -z "`which gawk`"
then
    log "ERROR: gawk is not installed but needed by this script. please install gawk."
    log "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
    exit 1
fi

if ! test -z "$CONFIGFILE"
then
    CONFIGS="$CONFIGFILE"
else
    CONFIGS="`find $CONFIGDIR -type f | grep -v \"defaults\"`"
fi

# -------------------

if ! test -z "$SHOW_QUOTA"
then
    log "  ----------------------------------------------------------------"
    log "  customer\t\t\tquota\ton disk\t\texceeded"
    log "  ----------------------------------------------------------------"
fi


# calculating stats for selected configurations
for i in $CONFIGS
do
    source "$i"

    
    CUSTBACKUP="$BACKUPDIR/$CUSTOMER"
    REPODIR="$CUSTBACKUP/archive"
    UPLOADDIR="$CUSTBACKUP/upload"

    if ! test -d "$CUSTBACKUP"
    then
        log "WARNING: Backup repository $CUSTBACKUP not found."

    else

        if ! test -z "$SHOW_STATS" || ! test -z "$SHOW_QUOTA"
        then

            JOB_SIZE_FULL="`du -sbl $CUSTBACKUP | cut -f 1 | tail -1`" 
            JOB_SIZE_FULL=`echo | gawk "{OFMT=\"%.3f\";print $JOB_SIZE_FULL/1024/1024/1024}"`
            UPLOAD_SIZE_FULL="`du -sbl $UPLOADDIR | cut -f 1 | tail -1`"
            UPLOAD_SIZE_FULL=`echo | gawk "{OFMT=\"%.3f\";print $UPLOAD_SIZE_FULL/1024/1024/1024}"`
            ARCHIVE_SIZE_FULL="`du -sbl $REPODIR | cut -f 1 | tail -1`"
            ARCHIVE_SIZE_FULL=`echo | gawk "{OFMT=\"%.3f\";print $ARCHIVE_SIZE_FULL/1024/1024/1024}"`
            DAILY_SIZE_FULL=`du -sbl $REPODIR/daily | cut -f 1 | tail -1`
            DAILY_SIZE_FULL=`echo | gawk "{OFMT=\"%.3f\";print $DAILY_SIZE_FULL/1024/1024/1024}"`
            WEEKLY_SIZE_FULL=`du -sbl $REPODIR/weekly | cut -f 1 | tail -1`
            WEEKLY_SIZE_FULL=`echo | gawk "{OFMT=\"%.3f\";print $WEEKLY_SIZE_FULL/1024/1024/1024}"`
            MONTHLY_SIZE_FULL=`du -sbl $REPODIR/monthly | cut -f 1 | tail -1`
            MONTHLY_SIZE_FULL=`echo | gawk "{OFMT=\"%.3f\";print $MONTHLY_SIZE_FULL/1024/1024/1024}"`
            YEARLY_SIZE_FULL=`du -sbl $REPODIR/yearly | cut -f 1 | tail -1`
            YEARLY_SIZE_FULL=`echo | gawk "{OFMT=\"%.3f\";print $YEARLY_SIZE_FULL/1024/1024/1024}"`

            JOB_SIZE_EFF="`du -sb $CUSTBACKUP | cut -f 1 | tail -1`"
            JOB_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $JOB_SIZE_EFF/1024/1024/1024}"`
            UPLOAD_SIZE_EFF="`du -sb $UPLOADDIR | cut -f 1 | tail -1`"
            UPLOAD_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $UPLOAD_SIZE_EFF/1024/1024/1024}"`
            ARCHIVE_SIZE_EFF="`du -sb $REPODIR | cut -f 1 | tail -1`"
            ARCHIVE_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $ARCHIVE_SIZE_EFF/1024/1024/1024}"`
            DAILY_SIZE_EFF=`du -sb $REPODIR/daily | cut -f 1 | tail -1`
            DAILY_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $DAILY_SIZE_EFF/1024/1024/1024}"`
            WEEKLY_SIZE_EFF=`du -sb $REPODIR/weekly | cut -f 1 | tail -1`
            WEEKLY_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $WEEKLY_SIZE_EFF/1024/1024/1024}"`
            MONTHLY_SIZE_EFF=`du -sb $REPODIR/monthly | cut -f 1 | tail -1`
            MONTHLY_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $MONTHLY_SIZE_EFF/1024/1024/1024}"`
            YEARLY_SIZE_EFF=`du -sb $REPODIR/yearly | cut -f 1 | tail -1`
            YEARLY_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $YEARLY_SIZE_EFF/1024/1024/1024}"`
            # ---

            if test "$JOB_SIZE_FULL" == "0.000"
            then
                JOB_RATIO="n/a"
            else
                JOB_RATIO=`echo | gawk "{OFMT=\"%.2f\";print $JOB_SIZE_FULL/$JOB_SIZE_EFF}"`
                JOB_RATIO="1:$JOB_RATIO"
            fi

            if test -z "$JOBQUOTA"; then JOBQUOTA="0"; fi
            if test "$JOBQUOTA" != "0"
            then
                JSE=`echo | gawk "{print $JOB_SIZE_EFF*1000}"`
                JQU=`echo | gawk "{print $JOBQUOTA*1000}"`

                L_Q=""
                L_E=""
                if test "$JSE" -gt "$JQU"
                then 
                    L_E="- WARNING: QUOTA LIMIT EXCEEDED"
                    L_Q="X"
                fi
            fi

            JOBQUOTA="$JOBQUOTA GB"
            if test "$JOBQUOTA" == "0 GB"; then JOBQUOTA="quota is disabled."; fi

        fi

        if ! test -z "$SHOW_HISTORY"
        then

            log "  -------------------------------------------------------------------------------------------------"
            log "  CUSTOMER: $CUSTOMER"
            log "  backup\t\tdate\t\ttype\t\traw\t\tnon-hard-link\tratio"
            log "  -------------------------------------------------------------------------------------------------"

            UPLOAD_SIZE_EFF="`du -sb $UPLOADDIR | cut -f 1 | tail -1`"
            UPLOAD_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $UPLOAD_SIZE_EFF/1024/1024/1024}"`

            for folder in `ls -tr $REPODIR/*/* | grep -e "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"`
            do
                folder=`echo $folder | cut -d ":" -f1`
                FOLDER_SIZE_EFF=`du -sb $folder | cut -f 1 | tail -1`
                FOLDER_SIZE_EFF=`echo | gawk "{OFMT=\"%.3f\";print $FOLDER_SIZE_EFF/1024/1024/1024}"`

                FOLDER_NAME=`basename $folder`

                FOLDER_SIZE_UPLOAD=`du -sb $UPLOADDIR $folder | grep $FOLDER_NAME | cut -f 1 | tail -1`
                FOLDER_SIZE_UPLOAD=`echo | gawk "{OFMT=\"%.3f\";print $FOLDER_SIZE_UPLOAD/1024/1024/1024}"`

                FOLDER_TYPE=`dirname $folder`
                FOLDER_TYPE=`basename $FOLDER_TYPE`

                RATIO=`echo | gawk "{OFMT=\"%.2f\";print $UPLOAD_SIZE_EFF/$FOLDER_SIZE_EFF}"`
             
                FOLDER_DATE=`stat $folder | grep Modify | awk '{ print $2 }'`

                log "  $FOLDER_NAME\t$FOLDER_DATE\t$FOLDER_TYPE\t\t$FOLDER_SIZE_EFF GB\t$FOLDER_SIZE_UPLOAD GB\t$RATIO"

            done
        fi

        if ! test -z "$SHOW_STATS"
        then
            log "  ------------------------------------"
            log "  CUSTOMER:  $CUSTOMER"
            log "  ratio      $JOB_RATIO"
            log "  quota      $JOBQUOTA $L_E"
            log "  ------------------------------------"
            log "             raw\t\ton disk"
            log "  ------------------------------------"
            log "  uploaded   $UPLOAD_SIZE_FULL GB\t$UPLOAD_SIZE_EFF GB"
            log "  archived   $ARCHIVE_SIZE_FULL GB\t$ARCHIVE_SIZE_EFF GB"
            log "  total      $JOB_SIZE_FULL GB\t$JOB_SIZE_EFF GB"
            log "  ------------------------------------"    
            log "  DAILY      $DAILY_SIZE_FULL GB\t$DAILY_SIZE_EFF GB"
            log "  WEEKLY     $WEEKLY_SIZE_FULL GB\t$WEEKLY_SIZE_EFF GB"
            log "  MONTHLY    $MONTHLY_SIZE_FULL GB\t$MONTHLY_SIZE_EFF GB"
            log "  YEARLY     $YEARLY_SIZE_FULL GB\t$YEARLY_SIZE_EFF GB"
            log "  ------------------------------------"
        fi

        

        if [ "${#CUSTOMER}" -lt "24" ] && [ "${#CUSTOMER}" -ge "16" ] ; then CUSTOMER="$CUSTOMER\t"     ; fi
        if [ "${#CUSTOMER}" -lt "16" ] && [ "${#CUSTOMER}" -ge "8"  ] ; then CUSTOMER="$CUSTOMER\t\t"   ; fi
        if [ "${#CUSTOMER}" -lt "8"  ] ;                                then CUSTOMER="$CUSTOMER\t\t\t" ; fi
        

        if ! test -z "$SHOW_QUOTA"
        then
            log "  $CUSTOMER\t$JOBQUOTA\t$JOB_SIZE_EFF GB\t$L_Q"
        fi
    fi
done

log "Statistics done."
log "-----------------------------------------------------------------------"

if [ "$QUOTAEMAIL" == "off" ]; then QUOTAEMAIL=""; fi

MAILMESSAGE="quota check"

if ! test -z "$QUOTAEMAIL"; then send "$MAILMESSAGE"; fi


# eof
